/*
 * name: popEditer.js
 * version: v0.0.5
 * update: 不弹出软键盘bug
 * date: 2015-11-08
 */
define('popEditer', function(require, exports, module) {
	seajs.importStyle('#popEditerForm .range{border:0;}\
	.range .text{display: block; min-width: 24px; text-align: center;  -webkit-box-flex: 0; -webkit-flex: 0; -moz-box-flex: 0; -moz-flex: 0; -ms-flex: 0; flex: 0;}	'
	,module.uri);
	//var $ = require('jquery');
	require('box');
	$.popEditer = function(palceholder, callback, prop) {
		if (palceholder === void(0) || (typeof(palceholder)==='function')) {
			return console.warn("editInput参数错误");
		}
		if (!$.isPlainObject(prop)) {
			prop = {
				datatype: '*'
			}
		} else if (!prop.datatype) {
			prop.datatype = '*';
		}
		var createProp = function() {
			var v = '';
			$.each(prop, function(i, e) {
				v += (' ' + i + '="' + e + '" ');
			});
			return v;
		}
		var createEditer = function(palceholder) {
			if (prop.tag && prop.tag != 'input') {
				switch (prop.tag) {
					case "range":
						if(prop['min']===void(0)){
							prop.min = Number(palceholder)/2;
						}
						if(prop['max']===void(0)){
							prop.max = Number(palceholder)*2;
						}
						return '<form id="popEditerForm"><div class="item range">\
						<span class="text">'+prop.min+'</span>\
						<input type="range" value="' + palceholder + '" id="getNewValue"' + createProp() + '>\
						<span class="text _max">'+palceholder+'</span>\
						</div></form>';
						break;
					default:
						return '<form id="popEditerForm"><label class="item item-input">\
						<' + prop.tag + ' id="getNewValue"' + createProp() + '>' + palceholder + '</' + prop.tag + '>\
		          </label></form>';
				}
			}
			return '<form id="popEditerForm"><label class="item item-input">\
	          <input type="text" value="' + palceholder + '" id="getNewValue"' + createProp() + '>\
	          </label></form>';
		};
		var popEditerForm, popEditerBox,
			getNewValue = function() {
				var _tag = $('#getNewValue').get(0).tagName.toLowerCase();
				if ('input,textarea,range'.indexOf(_tag) > -1) {
					return $('#getNewValue').val();
				}
				return $('#getNewValue').text();
			};
		//pop
		popEditerBox = $.box.confirm(createEditer(palceholder), function() {
			popEditerForm.submitForm(false);
		}, function(){
			$('#getNewValue').blur();
		}, {
			bar: false,
			onshow: function() {
				setTimeout(function(){
					$('#getNewValue').focus();
				},0);
				if($('#getNewValue[type=range]').length){
					var max = $($('#getNewValue').next('._max')),
						showMaxTime;
					$('#getNewValue').on('change',function(){
						max.text($('#getNewValue').val());
					});
				}
				require.async('validform', function() {
					popEditerForm = $('#popEditerForm').eq(0).Validform({
						beforeSubmit: function() {
							$.box.hide();
							setTimeout(function() {
								typeof(callback) === 'function' && callback(getNewValue());
							}, 0);
							return false;
						}
					});
				});
			}
		});
	}
});